﻿using BinConfig;
using JGSoft.U9CRTAPIEX.DTOs;
using JGSoft.U9CRTAPIEX.DTOs.Enum;
using JGSoft.U9CRTAPIEX.DTOs.RequestDTOs;
using JGSoft.U9CRTAPIEX.Public;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
using UFIDA.U9.AP.ForAPI;
using UFIDA.U9.Base;
using UFIDA.U9.Base.FlexField.ValueSet;
using UFIDA.U9.Base.Organization;
using UFIDA.U9.Base.UOM;
using UFIDA.U9.CBO.DTOs;
using UFIDA.U9.CBO.Enums;
using UFIDA.U9.CBO.FI.UI;
using UFIDA.U9.CBO.MFG.BOM;
using UFIDA.U9.CBO.MFG.CostElement;
using UFIDA.U9.CBO.MFG.Enums;
using UFIDA.U9.CBO.MFG.PMM;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.SCM.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.GL.Voucher;
using UFIDA.U9.ISV.Item.Proxy;
using UFIDA.U9.ISV.MFG.BOM.Proxy;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC.Common;
using UFSoft.UBF.Util.DataAccess;

namespace JGSoft.U9CRTAPIEX.Controllers
{
    public class KanBanController : ApiController
    {
        private readonly string _userCode;
        private readonly string _entCode;
        private readonly string _orgCode;
        public KanBanController()
        {
            JsonConfig _config = new JsonConfig();
            _userCode = _config.SelectToken("UserCode");
            _entCode = _config.SelectToken("EntCode");
            _orgCode = _config.SelectToken("OrgCode");
        }
        /// <summary>
        /// 获取当日看板数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> GetKanBanData()
        {
            ApiResult<string> result = new ApiResult<string>();
          
            using (BPForEngine bp = new BPForEngine())
            {
                ContextDTO tdto = new ContextDTO();
                tdto.UserCode = _userCode;//取接口传入的用户编码
                tdto.EntCode = _entCode;//取接口传入的企业编码
                tdto.CultureName = "zh-CN";//固定
                tdto.OrgCode = _orgCode;//取接口传入的组织编码
                tdto.WriteToContext();

                try
                {
                    using (ISession session = Session.Open())
                    {
                        KanBanDTO data = new KanBanDTO();

                        DateTime currentDate = DateTime.Now;
                        // 当月开始日期
                        DateTime startOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1);

                        // 当月结束日期
                        DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);

                        // 当年开始日期
                        DateTime startOfYear = new DateTime(currentDate.Year, 1, 1);

                        // 当年结束日期
                        DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);

                        //当年数据
                        DataSet Yeards;

                        DataAccessor.RunSP("proc_JG_AfterSaleOrder2",
                             new DataParamList{
                            new U9DataParam("@StartDate", startOfYear.ToShortDateString(),System.Data.ParameterDirection.Input),
                            new U9DataParam("@EndDate",endOfYear.ToShortDateString(),System.Data.ParameterDirection.Input)
                             }, out Yeards);
                        if (Yeards.Tables.Count > 0 && Yeards.Tables[0].Rows.Count > 0)
                        {
                            var YearData = Yeards.Tables[0].Rows[0];
                            data.ZCL = Convert.ToDecimal(YearData["已接订单数量"]);
                            data.LJDD = Convert.ToDecimal(YearData["已接订单笔数"]);
                            data.PZ = Convert.ToDecimal(YearData["品种数"]);
                        }
						//当月数据
						DataSet Monthds;

						DataAccessor.RunSP("proc_JG_AfterSaleOrder2",
							 new DataParamList{
							new U9DataParam("@StartDate", startOfMonth.ToShortDateString(),System.Data.ParameterDirection.Input),
							new U9DataParam("@EndDate",endOfMonth.ToShortDateString(),System.Data.ParameterDirection.Input)
							 }, out Monthds);

						if (Monthds.Tables.Count > 0 && Monthds.Tables[0].Rows.Count > 0)
						{
							var MonthData = Monthds.Tables[0].Rows[0];
							data.DDCTL = Convert.ToDecimal(MonthData["订单成套率"]);
							data.RKWBZ = Convert.ToDecimal(MonthData["入库未包装"]);
							data.WFY = Convert.ToDecimal(MonthData["未发运"]);
							data.DDBS = Convert.ToDecimal(MonthData["已接订单笔数"]);
                            data.DDSL = Convert.ToDecimal(MonthData["已接订单数量"]); 
                            data.WJBS = Convert.ToDecimal(MonthData["已完订单笔数"]); 
                            data.WJSL = Convert.ToDecimal(MonthData["已完订单数量"]); 
                            data.WWBS = Convert.ToDecimal(MonthData["未完订单笔数"]); 
                            data.WSSL = Convert.ToDecimal(MonthData["未收订单数量"]);
                            data.CQBS = Convert.ToDecimal(MonthData["超期订单笔数"]); 
                            data.CQSL = Convert.ToDecimal(MonthData["超期订单数量"]);
                        }

                        //当日数据
                        DataSet Dayds;

                        DataAccessor.RunSP("proc_JG_AfterSaleOrder3",
                             new DataParamList{
                            new U9DataParam("@RequireDate", currentDate.ToShortDateString(),System.Data.ParameterDirection.Input)
                             }, out Dayds);

                        if (Dayds.Tables.Count > 0 && Dayds.Tables[0].Rows.Count > 0)
                        {
                            var DayData = Dayds.Tables[0].Rows[0];
                            data.RKBS = Convert.ToDecimal(DayData["入库笔数"]);
                            data.RKSL = Convert.ToDecimal(DayData["入库数量"]);
                            data.BZBS = Convert.ToDecimal(DayData["包装笔数"]);
                            data.BZSL = Convert.ToDecimal(DayData["包装数量"]);
                        }

                        //明细数据
                        DataSet Detailds;

                        DataAccessor.RunSP("proc_JG_AfterSaleOrder4",
                             new DataParamList{
                             }, out Detailds);

                        if (Detailds.Tables.Count > 0 && Detailds.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow row in Detailds.Tables[0].Rows)
                            {
                                KanBanDetailDTO detailDTO = new KanBanDetailDTO();

                                detailDTO.RequireDate = Convert.ToString(row["交期"]);
                                detailDTO.SODocNo = Convert.ToString(row["内部销售订单号"]);
                                detailDTO.Customer = Convert.ToString(row["客户"]); 
                                detailDTO.LineNum = Convert.ToInt32(row["行数"]);
                                detailDTO.OrderQty = Convert.ToDecimal(row["订单数"]);
                                detailDTO.TotalRecievedQtyTU = Convert.ToDecimal(row["已实收数量"]);
                                detailDTO.PackingQty = Convert.ToDecimal(row["已包装"]);
                                detailDTO.StoreQty = Convert.ToDecimal(row["入库数量"]);
                                detailDTO.UnStoreQty = Convert.ToDecimal(row["未入库"]);
                                detailDTO.StoreUnPackingQty = Convert.ToDecimal(row["入库未包装"]);
                                detailDTO.ShipQtyInvAmount = Convert.ToDecimal(row["已发运"]);
                                detailDTO.UnShipQtyInvAmount = Convert.ToDecimal(row["未发运"]);
                                detailDTO.DelayDays = Convert.ToInt32(row["延迟天数"]);
                                data.Details.Add(detailDTO);
                            }
                           
                        }

                        result.Data = JsonConvert.SerializeObject(data);
                        return result;
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WriteLog(ex.Message + "\r\n" + ex.StackTrace, "All");
                    result.ResCode = 1;
                    result.ResMsg = ex.Message;
                    return result;
                }
            }
        }
    }
}
